﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace xna_game_engine.source
{
    public class EnemyManager
    {
        // Members
        private List<Enemy> mEnemyList;


        // Functions
        public EnemyManager()
        {
            mEnemyList = new List<Enemy>();
        }

        // TODO: Overload for random placement
        public void SpawnEnemy(Vector2 Pos)
        {
            Enemy temp = new Enemy("sprites\\enemies\\enemy", 100, GameGlobals.RNG.Next(10, 25), Pos);
            mEnemyList.Add(temp);
        }

        public void Update(GameTime gt)
        {
            foreach (Enemy ene in mEnemyList)
            {
                ene.Update(gt);
            }

            for (int i = 0; i < mEnemyList.Count; i++)
            {
                if (mEnemyList[i].IsDead)
                {
                    mEnemyList.RemoveAt(i);
                }
            }
        }

        public void Draw()
        {
            foreach (Enemy ene in mEnemyList)
            {
                ene.Draw();
            }
        }

        public Enemy GetEnemyClosestToPoint(Vector2 point)
        {
            Enemy target = null;
            float distance = 1000.0f;
            foreach (Enemy ene in mEnemyList)
            {
                float dist = Math.Abs(Vector2.Distance(ene.Position, point));
                if (dist < distance)
                {
                    // this enemy is closer than our last closest!
                    distance = dist;
                    target = ene;
                }
            }
            // At this point, target should be pointing at the closest enemy, or still null if there were no targets
            return target;
        }

        public Enemy GetEnemyClosestToPointWithinRange(Vector2 point, float range)
        {
            Enemy target = null;
            foreach (Enemy ene in mEnemyList)
            {
                float dist = Math.Abs(Vector2.Distance(ene.Position, point));
                if (dist <= range)
                {
                    target = ene;
                    break;
                }
            }
            // At this point, target should be pointing at the closest enemy within range, or still null if there were no targets
            return target;
        }
    }
}
